/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.wdm.parsers;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.wdm.beans.Crime;

/**
 *
 * @author thanasis
 */
public class PoliceParser {
    public static List<Crime> parse(double latitude, double longitude, String date, String crimeType, String userName, String password){
        List<Crime> crimeList = new ArrayList<Crime>();
        
        StringBuilder sb = new StringBuilder("http://policeapi2.rkh.co.uk/api/crimes-street/");
        sb.append(crimeType).append("?");
        sb.append("lat=").append(latitude);
        sb.append("&lng=").append(longitude);
        if (!date.equals("")) {
            sb.append("&date=").append(date);
        }

//                System.out.println(sb.toString());

        StringBuilder json = new StringBuilder();

        try {
            //Encoding the request url
            URL url = new URL(sb.toString());

            URLConnection conn = url.openConnection();
            String encoding = new sun.misc.BASE64Encoder().encode((userName + ":" + password).getBytes());
            conn.setRequestProperty("Authorization", "Basic " + encoding);
            conn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
            InputStream input = conn.getInputStream();


            //Reading the result of the request
//                    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            BufferedReader in = new BufferedReader(new InputStreamReader(input));
            String line = "";
            while ((line = in.readLine()) != null) {
//                        System.out.println(line);
                json.append(line);
            }
            in.close();
        } catch (IOException e) {
        }

        //Deserializing json into java objects using parser
        JsonParser parser = new JsonParser();

        //Getting the root element
        JsonArray crimesArray = parser.parse(json.toString()).getAsJsonArray();
        Iterator<JsonElement> crimeIterator = crimesArray.iterator();
        //HashMap<String, ArrayList<Crime>> crimesMap = new HashMap<>();
        while (crimeIterator.hasNext()) {
            JsonObject crimeJson = crimeIterator.next().getAsJsonObject();
            Crime crime = new Crime();

            crime.setCategory(crimeJson.get("category").getAsString());
            crime.setMonth(crimeJson.get("month").getAsString());
            crime.setContext(crimeJson.get("context").getAsString());
            crime.setLocation_type(crimeJson.get("location_type").getAsString());
            crime.setId(crimeJson.get("id").getAsString());

            JsonObject locationJson = crimeJson.getAsJsonObject("location");
            crime.setLatitude(locationJson.get("latitude").getAsDouble());
            crime.setLongitude(locationJson.get("longitude").getAsDouble());

            JsonObject streetJson = locationJson.getAsJsonObject("street");
            crime.setStreetId(streetJson.get("id").getAsString());
            crime.setStreetName(streetJson.get("name").getAsString());
            crimeList.add(crime);
        }
        return crimeList;
    }
}
